﻿using System;
using System.Collections.Generic;
using System.IO;
using wojilu.Apps.Qycms.Domain;
using wojilu.Web.Context;
using wojilu.Web.Mvc;

namespace wojilu.Web.Controller.Htmls
{
    public abstract class HtmlMaker
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(HtmlMaker));

        public static HomeMaker GetHome()
        {
            return ObjectContext.Create<HomeMaker>();
        }

        public static ListMaker GetList()
        {
            return ObjectContext.Create<ListMaker>();
        }

        public static ReadMaker GetRead()
        {
            return ObjectContext.Create<ReadMaker>();
        }

        protected abstract String GetDir(String lng);

        /// <summary>
        /// 循环生成列表页的所有页面
        /// </summary>
        /// <param name="sectionIdOrAppId">列表Id或AppId</param>
        /// <param name="lnkNormal">列表首页</param>
        /// <param name="recordCount">总数据量</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        protected int makeHtmlLoopAll(string lnkNormal, int recordCount, int pageSize, String lng, TypeList t, String htmlpath, Int32 pnumber)
        {

            // 获得所有分页的url(aspx动态页面)
            List<String> lnks = PageHelper.GetPageLinks(lnkNormal, recordCount, pageSize);
            var i = 1;
            foreach (String url in lnks)
            {
                if (i > pnumber)
                {
                    break;
                }
                makeListPageSingle(url, lng, t, htmlpath);
                i++;
            }
            return lnks.Count;
        }

        private void makeListPageSingle(String url, String lng, TypeList t, String htmlpath)
        {
            int pageNo = PageHelper.GetPageNoByUrl(url); // 当前页码
            String addr = url;
            String html = makeHtml(addr);
            String htmlPath = GetListPath(pageNo, lng, t, htmlpath);
            FileSystemObject.WriteFile(htmlPath, html);
            logger.Info("make html done=>" + htmlPath);
        }

        protected String CheckDir(String lng)
        {

            String dir = PathHelper.Map(GetDir(lng));

            if (Directory.Exists(dir)) return dir;
            Directory.CreateDirectory(dir);

            return dir;

        }

        protected String makeHtml(String addr)
        {
            return makeHtml(addr, new List<String>());
        }

        protected String makeHtml(String addr, List<String> nextMakeUrls)
        {
            StringWriter sw = new StringWriter();
            IWebContext webContext = MockWebContext.New(addr, sw);
            MvcContext ctx = new MvcContext(webContext);
            ctx.SetItem("_makeHtml", true);

            new CoreHandler().ProcessRequest(ctx);

            List<String> relativeUrls = ctx.GetItem("_relativeUrls") as List<String>;
            if (relativeUrls != null && relativeUrls.Count > 0)
            {
                nextMakeUrls.AddRange(relativeUrls);
            }

            return sw.ToString();
        }

        protected string GetListPath(int pageNo, String lng, TypeList t, String htmlPath)
        {
            if (pageNo == 1)
            {
                return Path.Combine(PathHelper.Map(htmlPath), YBBCommon.GetConfigStr("entrance_file") + "." + YBBCommon.GetConfigStr("file_fileex"));
            }
            else
            {
                return Path.Combine(PathHelper.Map(htmlPath), "p" + pageNo + "." + YBBCommon.GetConfigStr("file_fileex"));
            }

        }

    }
}
